/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.SistemaDAO;
import Modelo.Produto;
import Util.PAPUtil;
import java.math.BigDecimal;

/**
 *
 * @author computador
 */
public class CadProduto extends javax.swing.JFrame {

    LocProduto tp;
    PAPUtil papUtil = new PAPUtil();
    SistemaDAO sisDao = new SistemaDAO();
    Mensagem msn = new Mensagem(this);
    boolean novo = false;
    int idnovo = -1;
     public CadProduto(LocProduto tp, Produto prod) {
        initComponents();
        jTxtCodigo.setEnabled(false);
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
        this.tp = tp;
        setCampos(prod);
        if (prod.getControlado()==true){
        jPnRegistroMS.setVisible(true);
        }
        else jPnRegistroMS.setVisible(false);
       
    }
    
    public CadProduto(LocProduto tp, boolean novo) {
        initComponents();
        this.novo = novo;
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
        this.tp = tp;
        jPnRegistroMS.setVisible(false);
        idnovo = sisDao.ultimoCodigo("Modelo.Produto", "idproduto") + 1;
        jTxtCodigo.setText(String.valueOf(idnovo));
        jTxtCodigo.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTxtDescricao = new javax.swing.JTextField();
        jTxtCodigo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jBtCancelar = new javax.swing.JButton();
        jBtSalvar = new javax.swing.JButton();
        jLbErro = new javax.swing.JLabel();
        jPnPesquisaProduto = new javax.swing.JPanel();
        jTxtCodBarras = new javax.swing.JTextField();
        jTxtPreco = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jChControlado = new javax.swing.JCheckBox();
        jCBTipo = new javax.swing.JComboBox();
        jCBTipoDosag = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTxtQtd = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabelTipo = new javax.swing.JLabel();
        jPnRegistroMS = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jTxtNumRegistroMS = new javax.swing.JTextField();
        jMbPrincipal = new javax.swing.JMenuBar();
        jMnAcoes = new javax.swing.JMenu();
        jMiCadastrar = new javax.swing.JMenuItem();
        jMiSair = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Produto");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/remedios.jpg"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Produto"));
        jPanel2.setForeground(new java.awt.Color(255, 255, 255));

        jLabel2.setText("Descrição:");

        jTxtDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTxtDescricaoKeyTyped(evt);
            }
        });

        jTxtCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTxtCodigoFocusLost(evt);
            }
        });
        jTxtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTxtCodigoKeyTyped(evt);
            }
        });

        jLabel4.setText("Código:");

        jBtCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/X.png"))); // NOI18N
        jBtCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtCancelarActionPerformed(evt);
            }
        });

        jBtSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/V.png"))); // NOI18N
        jBtSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtSalvarActionPerformed(evt);
            }
        });

        jLbErro.setText("Hostipal Reat In Peace");

        jPnPesquisaProduto.setBackground(new java.awt.Color(255, 255, 255));
        jPnPesquisaProduto.setMinimumSize(new java.awt.Dimension(32767, 32767));
        jPnPesquisaProduto.setName("");

        jTxtCodBarras.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTxtCodBarrasKeyTyped(evt);
            }
        });

        jTxtPreco.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTxtPrecoKeyTyped(evt);
            }
        });

        jLabel8.setText("Preço:");

        jLabel7.setText("Código de Barras:");

        jChControlado.setBackground(new java.awt.Color(255, 255, 255));
        jChControlado.setText("Controlado");
        jChControlado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jChControladoActionPerformed(evt);
            }
        });

        jCBTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione ...", "Genérico", "Similar", "Ético" }));

        jCBTipoDosag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione ...", "Cápsula", "Comprimido", "Drágea", "Pílula", "Supositório", "Líquido" }));
        jCBTipoDosag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBTipoDosagActionPerformed(evt);
            }
        });

        jLabel6.setText("Tipo:");

        jLabel5.setText("Quantidade:");

        jTxtQtd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTxtQtdKeyTyped(evt);
            }
        });

        jLabel3.setText("Tipo Dosagem:");

        jPnRegistroMS.setBackground(new java.awt.Color(255, 255, 255));

        jLabel9.setText("Nº Registro MS:");

        jTxtNumRegistroMS.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTxtNumRegistroMSKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPnRegistroMSLayout = new javax.swing.GroupLayout(jPnRegistroMS);
        jPnRegistroMS.setLayout(jPnRegistroMSLayout);
        jPnRegistroMSLayout.setHorizontalGroup(
            jPnRegistroMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnRegistroMSLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTxtNumRegistroMS, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPnRegistroMSLayout.setVerticalGroup(
            jPnRegistroMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnRegistroMSLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnRegistroMSLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtNumRegistroMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPnPesquisaProdutoLayout = new javax.swing.GroupLayout(jPnPesquisaProduto);
        jPnPesquisaProduto.setLayout(jPnPesquisaProdutoLayout);
        jPnPesquisaProdutoLayout.setHorizontalGroup(
            jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxtCodBarras, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jChControlado, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPnRegistroMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                        .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jCBTipoDosag, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jCBTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTxtQtd, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTxtPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPnPesquisaProdutoLayout.setVerticalGroup(
            jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                        .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jCBTipoDosag)
                            .addComponent(jLabelTipo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTxtQtd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel5)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(jTxtPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))
                            .addComponent(jCBTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnPesquisaProdutoLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(jPnPesquisaProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jChControlado)
                            .addComponent(jTxtCodBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)))
                    .addComponent(jPnRegistroMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLbErro, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(46, 46, 46)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTxtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(333, 333, 333)
                                .addComponent(jBtCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jBtSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPnPesquisaProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 566, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(13, 13, 13)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jPnPesquisaProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtSalvar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLbErro))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(338, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMnAcoes.setText("Ações");

        jMiCadastrar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMiCadastrar.setText("Cadastrar");
        jMiCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMiCadastrarActionPerformed(evt);
            }
        });
        jMnAcoes.add(jMiCadastrar);

        jMiSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMiSair.setText("Sair");
        jMiSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMiSairActionPerformed(evt);
            }
        });
        jMnAcoes.add(jMiSair);

        jMbPrincipal.add(jMnAcoes);

        setJMenuBar(jMbPrincipal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jChControladoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jChControladoActionPerformed
        jPnRegistroMS.setVisible(jChControlado.isSelected());
    }//GEN-LAST:event_jChControladoActionPerformed

    private void jTxtCodigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtCodigoKeyTyped
        if (!papUtil.somenteNumero(evt, '0')) {
            evt.consume();
        }
        if (!papUtil.tamanhoMaximo(20, jTxtCodigo.getText().length())) {
            evt.consume();
        }
    }//GEN-LAST:event_jTxtCodigoKeyTyped

    private void jTxtDescricaoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtDescricaoKeyTyped
        if (!papUtil.tamanhoMaximo(100, jTxtDescricao.getText().length())) {
            evt.consume();
        }
    }//GEN-LAST:event_jTxtDescricaoKeyTyped

    private void jTxtQtdKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtQtdKeyTyped
        if (!papUtil.somenteNumero(evt, '0')) {
            evt.consume();
        }
        if (!papUtil.tamanhoMaximo(4, jTxtQtd.getText().length())) {
            evt.consume();
        }
    }//GEN-LAST:event_jTxtQtdKeyTyped

    private void jTxtPrecoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtPrecoKeyTyped
        if (!papUtil.somenteNumero(evt, '1')) {
            evt.consume();
        }
        if (!papUtil.tamanhoMaximo(15, jTxtPreco.getText().length())) {
            evt.consume();
        }
    }//GEN-LAST:event_jTxtPrecoKeyTyped

    private void jTxtNumRegistroMSKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtNumRegistroMSKeyTyped
        if (!papUtil.somenteNumero(evt, '0')) {
            evt.consume();
        }
        if (!papUtil.tamanhoMaximo(13, jTxtNumRegistroMS.getText().length())) {
            evt.consume();
        }
    }//GEN-LAST:event_jTxtNumRegistroMSKeyTyped

    private void jTxtCodBarrasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtCodBarrasKeyTyped
        if (!papUtil.somenteNumero(evt, '0')) {
            evt.consume();
        }
        if (!papUtil.tamanhoMaximo(13, jTxtCodBarras.getText().length())) {
            evt.consume();
        }
    }//GEN-LAST:event_jTxtCodBarrasKeyTyped

    private void jBtCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtCancelarActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jBtCancelarActionPerformed

    private void jBtSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtSalvarActionPerformed
        if (!verificaBranco()) {
            return;
        }
        sisDao.addUpObjeto(getCampos());
            
        Mensagem msn = new Mensagem(this, "Produto Cadastrado Com Susseço", "ok");
        msn.setVisible(true);
        limpaCampos();
        jLbErro.setText("Produto cadastrado com sucesso.");
    }//GEN-LAST:event_jBtSalvarActionPerformed

    private void jMiCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMiCadastrarActionPerformed
        if (!verificaBranco()) {
            return;
        }
        jLbErro.setText("Produto cadastrado com sucesso.");
        //this.setVisible(false);
    }//GEN-LAST:event_jMiCadastrarActionPerformed

    private void jMiSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMiSairActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jMiSairActionPerformed

    private void jCBTipoDosagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTipoDosagActionPerformed

        switch (jCBTipoDosag.getSelectedIndex()) {

            case 0: {
                jLabelTipo.setText("");
                break;
            }

            case 1: {
                jLabelTipo.setText("Cps");
                break;
            }
            case 2: {
                jLabelTipo.setText("Cp");
                break;
            }
            case 3: {
                jLabelTipo.setText("Drg");
                break;
            }
            case 4: {
                jLabelTipo.setText("Pl");
                break;
            }
            case 5: {
                jLabelTipo.setText("Sup");
                break;
            }
            case 6: {
                jLabelTipo.setText("Ml");
                break;
            }
        }
    }//GEN-LAST:event_jCBTipoDosagActionPerformed

    private void jTxtCodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTxtCodigoFocusLost
        //jTxtCodigo.setText(tb.UltimoCodigo(1));
    }//GEN-LAST:event_jTxtCodigoFocusLost
    public void limpaCampos() {
        
        jTxtCodigo.setText("");
        jTxtDescricao.setText("");
        jTxtPreco.setText("");
        jTxtQtd.setText("");
        jChControlado.setText("");
        jCBTipo.setSelectedIndex(0);
        jCBTipoDosag.setSelectedIndex(0);
        jTxtCodBarras.setText("");
        jPnRegistroMS.setVisible(false);
        
    }

  public void setCampos(Produto prod) {
        jTxtCodigo.setText(String.valueOf(prod.getIdproduto()));
        jTxtDescricao.setText(prod.getDescricao());
        jTxtPreco.setText(String.valueOf(prod.getPreco()));
        jTxtQtd.setText(String.valueOf(prod.getQuantidade()));
        jChControlado.setSelected(prod.getControlado());
        jCBTipo.setSelectedIndex(prod.getTipo());
        jCBTipoDosag.setSelectedIndex(prod.getTipoDosagem());
        jTxtCodBarras.setText(prod.getCodbarra());
        jTxtNumRegistroMS.setText(prod.getMinistsaude());
      }

    private boolean verificaBranco() {
        if (jTxtDescricao.getText().trim().equals("")) {
            jLbErro.setText("Campo Descrição é obrigatorio.");
            return false;
        }
        if (jCBTipoDosag.getSelectedIndex() <= 0) {
            jLbErro.setText("Campo tipo de dosagem é obrigatorio.");
            return false;
        }
        if (jCBTipo.getSelectedIndex() <= 0) {
            jLbErro.setText("Campo tipo é obrigatorio.");
            return false;
        }
        if (jTxtQtd.getText().trim().equals("")) {
            jLbErro.setText("Campo quantidade é obrigatorio.");
            return false;
        }
        if (jTxtPreco.getText().trim().equals("")) {
            jLbErro.setText("Campo preço é obrigatorio.");
            return false;
        }
        if (jTxtCodBarras.getText().trim().equals("")) {
            jLbErro.setText("Campo código de barras é obrigatorio.");
            return false;
        }

        return true;
    }
private Produto getCampos() {
        Produto prod = new Produto();  
        
        if(!novo){
        prod.setIdproduto(Integer.parseInt(jTxtCodigo.getText()));
        }
          prod.setDescricao(jTxtDescricao.getText());
          prod.setCodbarra(jTxtCodBarras.getText());
          prod.setMinistsaude(jTxtNumRegistroMS.getText());
          prod.setQuantidade(Integer.parseInt(jTxtQtd.getText()));
          prod.setTipo(jCBTipo.getSelectedIndex());
          prod.setTipoDosagem(jCBTipoDosag.getSelectedIndex());
          prod.setPreco(BigDecimal.valueOf(Double.parseDouble(jTxtPreco.getText())));
          prod.setControlado(jChControlado.isSelected()); 
          
          return prod;
         
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jBtCancelar;
    private javax.swing.JButton jBtSalvar;
    private javax.swing.JComboBox jCBTipo;
    private javax.swing.JComboBox jCBTipoDosag;
    private javax.swing.JCheckBox jChControlado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelTipo;
    private javax.swing.JLabel jLbErro;
    private javax.swing.JMenuBar jMbPrincipal;
    private javax.swing.JMenuItem jMiCadastrar;
    private javax.swing.JMenuItem jMiSair;
    private javax.swing.JMenu jMnAcoes;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPnPesquisaProduto;
    private javax.swing.JPanel jPnRegistroMS;
    private javax.swing.JTextField jTxtCodBarras;
    private javax.swing.JTextField jTxtCodigo;
    private javax.swing.JTextField jTxtDescricao;
    private javax.swing.JTextField jTxtNumRegistroMS;
    private javax.swing.JTextField jTxtPreco;
    private javax.swing.JTextField jTxtQtd;
    // End of variables declaration//GEN-END:variables
}
